<?php
namespace jy_xyb2b;
defined('BY_JYA') or exit('error');
/*
	商品接口
	https://open.xyb2b.com/client-api/document/app-001/menu-001/get_goods_category
*/
class goods extends \plugin{
	public function upcate(){
		$cat_list=$this->model('api')->getcate($parent_cat_id=0);
		foreach($cat_list as $item){
			$isadd=pdo_get('jy_xyb2b_group',array('category_id'=>$item['category_id'],'uniacid'=>$_SESSION['uniacid']));
			$data=array(
				'title'=>trim($item['category_name']),
				'category_id'=>intval($item['category_id']),
				'parent_cat_id'=>$parent_cat_id,
			);
			$id=intval($isadd['id']);
			if($id){
				pdo_update('jy_wxappjiaoyi_cate',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
			}else{	
				$data['uniacid']=$_SESSION['uniacid'];
				$data['createtime']=time();
				pdo_insert('jy_xyb2b_group',$data);
			}
		}
		return true;
	}
	public function updatagoods($category_id){
		$goods_list=$this->model('api')->getgoodslist($category_id);
		foreach($goods_list['list'] as $item){
			if($item['goods_code']){
				$isadd=pdo_get('jy_xyb2b_goods',array('goods_code'=>$item['goods_code'],'uniacid'=>$_SESSION['uniacid']));
			}
			$data=array(
				'title'=>trim($item['goods_name']),
				'goods_id'=>trim($item['goods_id']),
				'goods_code'=>trim($item['goods_code']),
				'brand_name'=>trim($item['brand_name']),
				'brand_logo'=>trim($item['brand_logo']),
				'origin_name'=>trim($item['origin_name']),
				'origin_icon'=>trim($item['origin_icon']),
				'trade_name'=>trim($item['trade_name']),
				'goods_img'=>trim($item['goods_img']),
				'price'=>trim($item['price']),
				'appid'=>$this->setting['appid'],
				'category_id3'=>$category_id
			);
			$id=intval($isadd['id']);
			if($id){
				pdo_update('jy_xyb2b_goods',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
			}else{	
				$data['uniacid']=$_SESSION['uniacid'];
				$data['createtime']=time();
				pdo_insert('jy_xyb2b_goods',$data);
			}
		}
		return true;
	}
	public function upgoodsitem($goods=array()){
		$goods_detail=$this->model('api')->getgoods_detail($goods['goods_code']);		
		$data=array(
				'category_id1'=>trim($goods_detail['category_id1']),
				'category_id2'=>trim($goods_detail['category_id2']),
				'category_id3'=>$goods_detail['category_id3']?:$goods['category_id3'],
				'content'=>trim($goods_detail['goods_detail']),
				'goods_thumb_image'=>ijson_encode($goods_detail['goods_thumb_image']),
				'sku_list'=>ijson_encode($goods_detail['sku_list']),
		);
		pdo_update('jy_xyb2b_goods',$data,array('id'=>$goods['id'],'uniacid'=>$_SESSION['uniacid']));
		$goods['sku_list']=$goods_detail['sku_list'];
		return $goods;
	}
	public function upsku_price($goods=array()){
		foreach($goods['sku_list'] as $k=>$v){
			$sku_price=$this->model('api')->getsku_price($v['sku_code']);
			$goods['sku_list'][$k]['sku_price']=$sku_price;
			$price=$price?:$sku_price[0]['price_list'][0]['batch_sell_price'];
		}
		$data=array(
			'sku_list'=>ijson_encode($goods['sku_list']),
			'price'=>$price,
		);
		pdo_update('jy_xyb2b_goods',$data,array('id'=>$goods['id'],'uniacid'=>$_SESSION['uniacid']));
		return $price;
	}
	public function upsku_total($goods=array()){
		foreach($goods['sku_list'] as $k=>$v){
			$sku_batch=$this->model('api')->getsku_total($v['sku_code']);
			$total=$sku_batch[0]['lock_stock_num'];
		}
		$data=array(
			'total'=>$total,
		);
		pdo_update('jy_xyb2b_goods',$data,array('id'=>$goods['id'],'uniacid'=>$_SESSION['uniacid']));
		return $price;
	}
}